﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FisheryPlatform.Bussiness.WebRuntime;
using FisheryPlatform.Bussiness.LoginCookie;
using System.Drawing;
using FisheryPlatform.Bussiness;
using FisheryPlatform.SiteConfig.FileStore;
using FisheryPlatform.Bussiness.FileStore;
using System.IO;

namespace FisheryPlatform.Web.WWW.Ajax
{
    /// <summary>
    /// ContactInfo 的摘要说明
    /// </summary>
    public class ContactInfo : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            context.Response.ContentType = "image/JPEG";
            if (context.Request.UrlReferrer != null)
            {
                var pathList = context.Request.UrlReferrer.AbsolutePath.Replace("/p","").Split('.');
                var path = pathList[0] + ".jpg";
                byte[] buffer = ContactInfoImageUploadProvider.GetFileContent(path);
                context.Response.OutputStream.Write(buffer, 0, buffer.Length);
            }
        }

        private void ContactPhone()
        {
            #region
            var user = new LoginUserInfo().AdminUserInfo;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
            Random rdm = new Random();
            System.Drawing.Brush brush = new System.Drawing.SolidBrush(Color.Red);
            System.Drawing.Pen pen = new Pen(brush);
            Graphics gh = null;
            Bitmap img = null;
            try
            {
                var data = (user.Mobile + (string.IsNullOrEmpty(user.Phone) ? "" : "," + user.Phone)).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //data += "," + data;
                var width = (data.Length > 2 ? 23 : 11) * 10;
                //if (data.Length > 20 && data.Length < 40)
                //{
                //    width -= 10;
                //}
                //else if (data.Length >= 40)
                //{
                //    width -= 25;
                //}
                var groupCount = Convert.ToInt32(Math.Ceiling((decimal)data.Length / 2));
                img = new Bitmap(width, groupCount * 24);
                gh = Graphics.FromImage(img);
                gh.Clear(Color.Beige);

                if (data.Length > 2)
                {
                    for (var i = 1; i <= groupCount; i++)
                    {
                        gh.DrawString(string.Format("{0},{1}", data[0], data[1]), new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 3));
                    }

                    gh.DrawString(data[2], new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 24));
                }
                else
                {
                    gh.DrawString(string.Join(",", data), new Font("Arial", 13, FontStyle.Bold), brush, new PointF(0, 3));
                }
                for (int i = 0; i < 100; i++)
                {
                    int num = rdm.Next(0, color.Length);
                    int x = rdm.Next(0, img.Width);
                    int y = rdm.Next(0, img.Height);
                    img.SetPixel(x, y, color[num]);
                }
                img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                ExceptionMessage.Record(ex);
            }
            finally
            {
                gh.Dispose();
                img.Dispose();
            }
            Response.End();
            //context.Response.Write(user.Mobile + (string.IsNullOrEmpty(user.Phone) ? "" : "," + user.Phone));
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}